package com.plant.action;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.plant.entity.UgcNewthings;
import com.plant.entity.UgcUser;
import com.plant.service.FuzzySearchService;
import com.plant.service.HelpService;
import com.plant.service.HomePageService;
import com.plant.service.UgcUserContrallerService;
import com.plant.service.UserServic;
import com.plant.tool.CalculateDate;
import com.plant.tool.DateUtils;

/**涉及的表：表1：ugc_user；author TOMlee
 */
@Controller
@RequestMapping(value = "/ugc_user")
public class UgcUserContraller {

	@Resource(name="ugcUserContrallerServiceImpl")
	private UgcUserContrallerService ugcUserContrallerService;
	@Resource(name = "helpServiceImpl")//spring 注入依赖管理 注入管理接口service
	private HelpService helpService;
	@Resource(name = "userInfoImlp")
	private UserServic userServic;
	@Resource(name = "homePageServiceImpl")
	private HomePageService homePageService;
	@Resource(name = "fuzzySearchServiceImpl")//--模糊搜索的接口方法
	private FuzzySearchService fuzzySearchService;

	@RequestMapping(value = "userinf")//用户个人详情页面
	@ResponseBody
	public  Map<String, Object> UserInf(Integer current_user_id,Integer be_viewed_user_id,UgcUser u,HttpServletResponse response,String secret,String pust_val) {
		response.setHeader("access-control-allow-origin","*");
		Map<String,Object> map=new HashMap<String, Object>();
		if(pust_val==null||secret==null||current_user_id==null||be_viewed_user_id==null){
			map.put("message", "参数为空");
			map.put("state", 0);
			return map;
		}else{
			String mds=Md.MdSecret(pust_val+"flower_duigu2016");
			if(secret.endsWith(mds)){
				u.setId(be_viewed_user_id);
				List<Object> userinf=new ArrayList<Object>();
				List<Object> temp=new ArrayList<Object>();
				userinf=userServic.getByuid(u);
				if(be_viewed_user_id==current_user_id){
					map.put("mes", "查看自己的个人信息");
				}else{
					List<Object> list1=new ArrayList<Object>();
					List<Object> list2=new ArrayList<Object>();
					list1=userServic.isfriend(current_user_id, be_viewed_user_id);
					list2=userServic.IsNotfriend(be_viewed_user_id, current_user_id);
					if(list1.size()>0||list2.size()>0){
						map.put("isfriend",1);
					}else{
						map.put("isfriend", 0);
					}
				}
				for (Object object : userinf) {
					Map<String,Object> map1=new HashMap<String, Object>();
					UgcUser user=(UgcUser) object;
					map1.put("user_id", user.getId());
					map1.put("nickname", user.getNickname());
					map1.put("photo", user.getPhoto());
					map1.put("sex", user.getSex());
					map1.put("city", user.getCity());
					map1.put("province", user.getProvince());
					map1.put("signature", user.getSignature());
					map1.put("background_image", user.getBackgroundImage());
					map1.put("newthings_count", ugcUserContrallerService.UserNewthingsCount(user));
					long faqcount=ugcUserContrallerService.UserFaqCount(user);
					long faqanswercount=ugcUserContrallerService.UserFaqAnswerCount(user);
					map1.put("myanswer_count", faqcount+faqanswercount);
					map1.put("medal_count", 0);
					temp.add(map1);
				}
				map.put("userinf", temp);
				return map;
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 1);
				return map;
			}
		}
	}

	@RequestMapping(value ="my_column")//我的栏目1.1.2
	@ResponseBody
	public Map<Object,Object> lanmu(Integer user_id,HttpServletResponse response,UgcUser user,String secret,String pust_val){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object,Object>();
		if(secret==null||pust_val==null){
			map.put("messgage", "请查看参数是否正确");
			map.put("state", 0);
			return map;
		}else{
			String mds=Md.MdSecret(pust_val+"flower_duigu2016");
			if(secret.endsWith(mds)){
				user.setId(user_id);
				List<Object> list2=new ArrayList<Object>();
				List<Object> list3=new ArrayList<Object>();
				list2=userServic.getByuid(user);
				long l=userServic.myfriendcount(user);
				for (Object object : list2) {
					Map<String, Object> temp=new HashMap<String, Object>();
					UgcUser u=(UgcUser) object;
					temp.put("user_id", u.getId());
					temp.put("nickname", u.getNickname());
					temp.put("photo", u.getPhoto());
					temp.put("MyId", u.getMyId());
					long count_like=fuzzySearchService.OneUserAnswerBylikeCount(u);
					map.put("count_like", count_like);
					list3.add(temp);
				}
				map.put("friendcount",l);
				map.put("userinf",list3);
				map.put("message", "请求我的栏目成功");
				map.put("state", 1);
				return map;
			}else{
				map.put("error", "秘钥错误");
				map.put("state", 1);
				return map;
			}
		}
	}

	@RequestMapping(value = "user_answers_beliked")//7用户回答被点赞详情
	@ResponseBody
	public  Map<String, Object> UserBeLikeInf(Integer current_user_id,UgcUser u,
			String secret,String pust_val,Integer page,String time) {
		Map<String,Object> map=new HashMap<String, Object>();
		if(pust_val==null||secret==null||current_user_id==null||page==null||time==null){
			map.put("message", "参数为空");
			map.put("state", 0);
			return map;
		}else{
			String mds=Md.MdSecret(pust_val+"flower_duigu2016");
			if(secret.endsWith(mds)){
				u.setId(current_user_id);
				List<Map<String, Object>> userinf=new ArrayList<Map<String, Object>>();
				List<Map<String, Object>> userinfs=new ArrayList<Map<String, Object>>();
				userinf=ugcUserContrallerService.UserByInf(u,page,time);
				userinfs=ugcUserContrallerService.UserByInf(u,page+1,time);
				if(userinfs.size()>0){
					map.put("hasmore", 1);
				}else{
					map.put("hasmore", 0);
				}
				map.put("userinf", userinf);
				return map;
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 1);
				return map;
			}
		}
	}

	@RequestMapping(value ="my_friend")//我的好友列表
	@ResponseBody
	public Map<Object,Object> RemindWho(Integer user_id,UgcUser u,
			HttpServletResponse response,String secret,
			String pust_val,Integer page){
		response.setHeader("access-control-allow-origin","*");
		Map<Object, Object> map=new HashMap<Object, Object>();
		if(secret==null||pust_val==null||user_id==null||page==null){
			map.put("message", "传参错误");
			map.put("state", 0);
			return map;
		}else{
			Md md=new Md();
			String mds=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mds)){
				u.setId(user_id);
				List<Object> list3=new ArrayList<Object>();
				List<Object> content=new ArrayList<Object>();
				List<Object[]> list=new ArrayList<Object[]>();
				List<Object> list4=new ArrayList<Object>();
				list3=ugcUserContrallerService.MyfriendIds(u, page);
				list4=ugcUserContrallerService.MyfriendIds(u, page+1);
				if(list4.size()>0){
					map.put("hasmore", 1);
				}else{
					map.put("hasmore", 0);
				}
				Object ids=StringUtils.join(list3.toArray(),",");
				if(ids==null){
					System.out.println("你没有好友");
				}else{
					list=userServic.UserThreeInf(ids);
					for (int i = 0; i < list.size(); i++) {
						Map<Object,Object> map1=new HashMap<Object, Object>();
						Object[] obj=list.get(i);
						map1.put("user_id", obj[0]);
						map1.put("nickname", obj[1]);
						map1.put("user_photo", obj[2]);
						map1.put("signature", obj[3]);
						content.add(map1);
					}
					map.put("content", content);
				}
				map.put("message", "查询成功");
				map.put("state", 1);
				return map;
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 0);
				return map;
			}
		}
	}

	@RequestMapping(value ="my_question_answer")//我的好友列表
	@ResponseBody
	public Map<Object,Object> MyQuestionAnswer(Integer user_id,UgcUser u,
			HttpServletResponse response,String secret,
			String pust_val,Integer page,String time){
		response.setHeader("access-control-allow-origin","*");
		Map<Object, Object> map=new HashMap<Object, Object>();
		if(secret==null||pust_val==null||user_id==null||page==null||time==null){
			map.put("message", "传参错误");
			map.put("state", 0);
			return map;
		}else{
			Md md=new Md();
			String mds=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mds)){
				u.setId(user_id);
				//				String time="2016-12-26 21:54:39";
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				List<Map<String, Object>> qanda=new ArrayList<Map<String,Object>>();
				List<Map<String, Object>> moreqanda=new ArrayList<Map<String,Object>>();
				List<Object> answer=new ArrayList<Object>();
				List<Object> answerids=new ArrayList<Object>();
				List<Object> faqid=new ArrayList<Object>();
				List<Object> faqtitle=new ArrayList<Object>();
				qanda=userServic.ListMyQuestionAndAnswer(u, page, time);
				moreqanda=userServic.ListMyQuestionAndAnswer(u, page+1, time);
				if(moreqanda.size()>0){
					map.put("hasmore", 1);
				}else{
					map.put("hasmore", 0);
				}
				for (Map<String, Object> map2 : qanda) {
					Integer content_type=Integer.parseInt(map2.get("content_type").toString());
					if(content_type==1){
						Object temptime=map2.get("create_time");
						Object id_key=map2.get("id");
						String t=sim.format(temptime);
						long l=DateUtils.StaticgetUnixStamps(t);
						answer=fuzzySearchService.OneFaqAnswer(id_key);
						if(answer.size()==0){
							map2.put("answer", "");
						}else{
							map2.put("answer", answer.get(0));
						}
						answerids=fuzzySearchService.AllAnswerIds(id_key);
						String ids=StringUtils.join(answerids.toArray(),",");
						if(ids.equals(null)||ids.equals("")){
							map2.put("count_like", 0);
						}else{
							map2.put("count_like", fuzzySearchService.CountAllFaqAnswerCount(ids));
						}
						map2.put("create_time",DateUtils.StaticconvertTimeToFormat(l));
						map2.put("answer_count", helpService.friendfaqcount(u, id_key));
					}else{
						Object uid=map2.get("user_id");
						Object temptime=map2.get("create_time");
						Object anid=map2.get("id");
						faqid=userServic.FindByAnswerId(anid);
						Object id_key=faqid.get(0);
						String content=map2.get("title").toString();
						map2.put("answer", content);
						map2.remove("title");
						faqtitle=userServic.FindfaqById(id_key);
						map2.put("title", faqtitle.get(0));
						UgcUser user=(UgcUser) userServic.UserPartInf(uid);
						map2.put("nickname", user.getNickname());
						map2.put("photo", user.getPhoto());
						String t=sim.format(temptime);
						long l=DateUtils.StaticgetUnixStamps(t);
						answerids=fuzzySearchService.AllAnswerIds(id_key);
						String ids=StringUtils.join(answerids.toArray(),",");
						if(ids.endsWith("")||ids.equals(null)){
							map2.put("count_like", 0);
						}else{
							map2.put("count_like", fuzzySearchService.CountAllFaqAnswerCount(ids));
						}
						map2.put("create_time",DateUtils.StaticconvertTimeToFormat(l));
						map2.put("answer_count", helpService.friendfaqcount(u, id_key));
					}
				}
				map.put("content", qanda);
				map.put("message", "查询成功");
				map.put("state", 1);
				return map;
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 0);
				return map;
			}
		}
	}


	@RequestMapping(value = "list_editor_userinf")//查看编辑用户资料
	@ResponseBody
	public  Map<String, Object> list_editor_userinf(Integer user_id,UgcUser u,HttpServletResponse response,
			String secret,String pust_val) {
		response.setHeader("access-control-allow-origin","*");
		Map<String,Object> map=new HashMap<String, Object>();
		if(pust_val==null||secret==null||user_id==null){
			map.put("message", "参数为空");
			map.put("state", 0);
			return map;
		}else{
			String mds=Md.MdSecret(pust_val+"flower_duigu2016");
			if(secret.endsWith(mds)){
				u.setId(user_id);
				List<Object> userinf=new ArrayList<Object>();
				List<Object> temp=new ArrayList<Object>();
				userinf=userServic.getByuid(u);
				for (Object object : userinf) {
					Map<String,Object> map1=new HashMap<String, Object>();
					UgcUser user=(UgcUser) object;
					map1.put("user_id", user.getId());
					map1.put("nickname", user.getNickname());
					map1.put("photo", user.getPhoto());
					map1.put("sex", user.getSex());
					map1.put("city", user.getCity());
					map1.put("province", user.getProvince());
					map1.put("signature", user.getSignature());
					map1.put("background_image", user.getBackgroundImage());
					temp.add(map1);
				}
				map.put("userinf", temp);
				return map;
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 1);
				return map;
			}
		}
	}

	@RequestMapping(value = "list_friend_apply")//查看好友请求列表
	@ResponseBody
	public  Map<String, Object> ListFriendApply(Integer page,Integer user_id,UgcUser u,HttpServletResponse response,
			String secret,String pust_val) {
		response.setHeader("access-control-allow-origin","*");
		Map<String,Object> map=new HashMap<String, Object>();
		if(pust_val==null||secret==null||user_id==null){
			map.put("message", "参数为空");
			map.put("state", 0);
			return map;
		}else{
			String mds=Md.MdSecret(pust_val+"flower_duigu2016");
			if(secret.endsWith(mds)){
				u.setId(user_id);
				String time=CalculateDate.getyesterday();
				List<Map<String, Object>> apply=new ArrayList<Map<String,Object>>();
				List<Map<String, Object>> more_apply=new ArrayList<Map<String,Object>>();
				List<Map<String, Object>> today=new ArrayList<Map<String,Object>>();
				List<Map<String, Object>> yesterday=new ArrayList<Map<String,Object>>();
				apply=userServic.ListFriendApply(page, u);
				more_apply=userServic.ListFriendApply(page, u);
				if(more_apply.size()>0){
					map.put("hasmore", 1);
				}else{
					map.put("hasmore", 0);
				}
				for (Map<String, Object> map2 : apply) {
					map2.remove("COUNT(DISTINCT a.user_id)");
					String create_time=map2.get("create_time").toString();
					Integer state=Integer.parseInt(map2.get("state").toString());
					if(state<2){
						if(create_time.compareTo(time)>0){
							today.add(map2);
						}else{
							yesterday.add(map2);
						}
					}
				}
				map.put("past_appply", yesterday);
				map.put("nowaday_apply", today);
				return map;
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 1);
				return map;
			}
		}
	}


	@RequestMapping(value = "user_newthings")//查看用户新鲜事
	@ResponseBody
	public  Map<String, Object> UserNewthings(Integer page,String first_page_time,Integer user_id,UgcUser u,HttpServletResponse response,
			String secret,String pust_val) {
		response.setHeader("access-control-allow-origin","*");
		Map<String,Object> result=new HashMap<String, Object>();
		if(pust_val==null||secret==null||user_id==null||page==null||first_page_time==null){
			result.put("message", "参数为空");
			result.put("state", 0);
			return result;
		}else{
			String mds=Md.MdSecret(pust_val+"flower_duigu2016");
			if(secret.endsWith(mds)){
				//				u.setId(147);
				u.setId(user_id);
				List<Map<String, Object>> newthings=new ArrayList<Map<String,Object>>();
				List<Map<String, Object>> newthing=new ArrayList<Map<String,Object>>();
				newthings=userServic.UserNewthings(u,first_page_time,page);
				newthing=userServic.UserNewthings(u,first_page_time,page+1);
				int len=newthing.size();
				if(len>0){
					result.put("hasmore", 1);
				}else{
					result.put("hasmore", 0);
				}
				Map<String, List<Map<String, Object>>> temp=new LinkedHashMap<String, List<Map<String,Object>>>();//
				for (Map<String, Object> tempMap : newthings) {
					Timestamp time1 = (Timestamp) tempMap.get("create_time");//通过键值    获取时间
					Object id_key=tempMap.get("id_key");
					tempMap.put("images", userServic.NewThingsImages(id_key));
					String tempTime = DateUtils.timeStamp(time1.getTime());
					if (temp.containsKey(tempTime)) {
						temp.get(tempTime).add(tempMap);
					}else{
						List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
						list.add(tempMap);
						temp.put(tempTime, list);
					}
				}
				List<Map<String, Object>> timelines=new ArrayList<Map<String,Object>>();
				for (String time : temp.keySet()) {
					Map<String, Object> timeline = new HashMap<String, Object>();
					List<Map<String, Object>> timeList = temp.get(time);
					for (Map<String, Object> map : timeList) {
						map.remove("create_time");
					}
					timeline.put("day", time);
					timeline.put("cotent", timeList);
					timelines.add(timeline);
				}
				result.put("timeline", timelines);
				return result;
			}else{
				result.put("message", "秘钥错误");
				result.put("state", 1);
				return result;
			}
		}
	}


	@RequestMapping(value = "user_newthings_inf")//一条新鲜事详情
	@ResponseBody
	public  Map<String, Object> UserNewthingsInf(Integer user_id,Integer newthing_content_id,UgcNewthings un,
			String secret,String pust_val,UgcUser u) {
		Map<String,Object> result=new HashMap<String, Object>();
		if(pust_val==null||secret==null||newthing_content_id==null||user_id==null){
			result.put("message", "参数为空");
			result.put("state", 0);
			return result;
		}else{
			String mds=Md.MdSecret(pust_val+"flower_duigu2016");
			if(secret.endsWith(mds)){
				un.setId(newthing_content_id);
				u.setId(user_id);
				List<Map<String, Object>> oneinf=new ArrayList<Map<String,Object>>();
				List<Map<String, Object>> likeinf=new ArrayList<Map<String,Object>>();
				List<Object[]> commentlist=new ArrayList<Object[]>();
				List<Object> commentlists=new ArrayList<Object>();
				List<Object> friendids=new ArrayList<Object>();
				friendids=userServic.MyFriendIds(u);
				friendids.add(user_id);
				Object ids=StringUtils.join(friendids.toArray(),",");
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				oneinf=userServic.OneNewthingsInf(un);
				for (Map<String, Object> map : oneinf) {
					Object temptime=map.get("create_time");
					String time=sim.format(temptime);
					map.put("create_time", DateUtils.StaticTopicCommentTime(DateUtils.StaticgetUnixStamps(time)));
					Object id_key=map.get("id_key");
					map.put("images", userServic.NewThingsImages(id_key));
					likeinf=userServic.UserNewsthingsByLike(id_key);
					int len=likeinf.size();
					map.put("count_like", len);
					map.put("like_userinf", likeinf);
					commentlist=homePageService.CommentList(ids,id_key);
					if(commentlist.size()!=0){
						for (int j = 0; j < commentlist.size(); j++) {
							Map<Object,Object> map5=new HashMap<Object,Object>();
							Object[] com=commentlist.get(j);
							map5.put("id_key",com[0]);
							map5.put("from_user_id", String.valueOf(com[1]));
							map5.put("from_user_name", com[4]);
							map5.put("content", com[2]);
							if(com[5].equals(0)){
								map5.put("to_user_id",String.valueOf(0));
								map5.put("to_user_name", "");
							}else{
								map5.put("to_user_id", String.valueOf(com[5]));
								UgcUser username=(UgcUser) userServic.UserPartInf(com[5]);
								map5.put("to_user_name", username.getNickname());
							}
							String commenttime=sim.format(com[6]);
							map5.put("create_time", DateUtils.StaticTopicCommentTime(DateUtils.StaticgetUnixStamps(commenttime)));
							commentlists.add(map5);
						}
					}
					map.put("comment_list",commentlists);
				}
				result.put("one_newthinginf", oneinf);
				return result;
			}else{
				result.put("message", "秘钥错误");
				result.put("state", 1);
				return result;
			}
		}
	}




}
